package com.enation.app.javashop.seller.api.aftersale;

import com.enation.app.javashop.core.aftersale.AftersaleErrorCode;
import com.enation.app.javashop.core.aftersale.model.dto.RefundDTO;
import com.enation.app.javashop.core.aftersale.model.dto.RefundDetailDTO;
import com.enation.app.javashop.core.aftersale.model.vo.FinanceRefundApprovalVO;
import com.enation.app.javashop.core.aftersale.model.vo.RefundQueryParamVO;
import com.enation.app.javashop.core.aftersale.model.vo.SellerRefundApprovalVO;
import com.enation.app.javashop.core.aftersale.service.AfterSaleManager;
import com.enation.app.javashop.framework.context.UserContext;
import com.enation.app.javashop.framework.database.Page;
import com.enation.app.javashop.framework.exception.ServiceException;
import com.enation.app.javashop.framework.security.model.Seller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author zjp
 * @version v7.0
 * @Description 售后相关API
 * @ClassName AfterSaleSellerController
 * @since v7.0 上午9:38 2018/5/10
 */
@Api(description = "售后相关API")
@RestController
@RequestMapping("/seller/after-sales")
@Validated
public class AfterSaleSellerController {

    @Autowired
    private AfterSaleManager afterSaleManager;

    @ApiOperation(value = "卖家审核退款/退货", response = SellerRefundApprovalVO.class)
    @PostMapping(value = "/audits/{sn}")
    @ApiImplicitParam(name = "sn", value = "退款单sn", required =true, dataType = "String" ,paramType="path")
    public SellerRefundApprovalVO audit(@Valid SellerRefundApprovalVO refundApproval, @PathVariable("sn") String sn) {
        refundApproval.setSn(sn);
        afterSaleManager.approval(refundApproval);
        return refundApproval;
    }

    @ApiOperation(value = "卖家入库操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "退款单编号", required =true, dataType = "String" ,paramType="path"),
            @ApiImplicitParam(name = "remark", value = "入库备注", required =false, dataType = "String" ,paramType="query")
    })
    @PostMapping(value = "/stock-ins/{sn}")
    public String stockIn(@PathVariable("sn") String sn,String remark){
        afterSaleManager.sellerStockIn(sn,remark);
        return "";
    }

    @ApiOperation(value = "卖家退款", response = FinanceRefundApprovalVO.class)
    @PostMapping(value = "/refunds/{sn}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "退款(货)编号", required =true, dataType = "String" ,paramType="path"),
            @ApiImplicitParam(name = "remark", value = "退款备注", required =false, dataType = "String" ,paramType="query")
    })
    public String sellerRefund(@PathVariable("sn") String sn,String remark){
        this.afterSaleManager.sellerRefund(sn,remark);
        return "";
    }

    @ApiOperation(value = "卖家查看退款(货)详细", response = RefundDetailDTO.class)
    @ApiImplicitParam(name = "sn", value = "退款(货)编号", required =true, dataType = "String" ,paramType="path")
    @GetMapping(value = "/refunds/{sn}")
    public RefundDetailDTO sellerDetail(@PathVariable("sn")  String sn  ) {
        Seller seller = UserContext.getSeller();
        RefundDetailDTO detail  = this.afterSaleManager.getDetail(sn);
        if(!detail.getRefund().getSellerId().equals(seller.getSellerId())){
            throw new ServiceException(AftersaleErrorCode.E603.name(),"退款单不存在");
        }
        return detail;
    }

    @ApiOperation(value = "卖家查看退款(货)列表", response = RefundDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_no" , value = "页码" , required = true , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page_size" , value = "分页数" , required = true , dataType = "int", paramType = "query")
    })
    @GetMapping(value = "/refunds")
    public Page sellerDetail(RefundQueryParamVO queryParam,@ApiIgnore @NotNull(message = "页码不能为空") Integer pageNo, @ApiIgnore @NotNull(message = "每页数量不能为空") Integer pageSize ) {

        Seller seller = UserContext.getSeller();
        queryParam.setPageNo(Integer.valueOf(pageNo));
        queryParam.setPageSize(pageSize);
        queryParam.setSellerId( seller.getSellerId());
        return this.afterSaleManager.query(queryParam);
    }
}
